/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pruebas.DNIe;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.security.cert.*;
import java.security.*;
import java.util.Date;

public class accederDNIe extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        out.println("<HTML>");
        out.println("<BODY>");
        out.println("<H3>Informacion de certificado de cliente</H3>");
        Object o = req.getAttribute("javax.servlet.request.X509Certificate");
        if (o == null) {
            out.println("<B>No hay certificado de cliente</B>");
        } else {
            try {
                X509Certificate[] certs = (X509Certificate[]) o;
                X509Certificate cert;
                for (int i = 0; i < certs.length; i++) {
                    cert = certs[i];
                    out.println("Cert number: " + i + "<BR>");
                    Principal dn = cert.getSubjectDN();
                    out.println("SubjectDN: " + dn.toString() + "<BR>");
                    Principal issuer = cert.getIssuerDN();
                    out.println("IssuerDN: " + issuer.toString() + "<BR>");
                    try {
                        cert.checkValidity();
                        out.println("Validity: certificate is valid");
                    } catch (CertificateExpiredException ex) {
                        out.println("Validity: certificate is expired");
                    } catch (CertificateNotYetValidException ex) {
                        out.println("Validity: certificate is not yet valid");
                    }
                    out.println("<BR>");
                    Date d = cert.getNotBefore();
                    out.println("From " + d);
                    d = cert.getNotAfter();
                    out.println(" to " + d + "<BR>");
                }
            } catch (Exception ex) {
                out.println("<B>Exception retrieving cert: " + ex.toString() + "</B>");
            }
        }
        out.println("</BODY>");
        out.println("</HTML>");
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        doGet(req, res);
    }
}
